﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using System.Text;

namespace OA.Controllers
{
    public class DiscountController : Controller
    {
        //
        // GET: /Discount/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Discount()
        {
            return View();
        }
        //加载数据
        public ActionResult Load(FormCollection fc)
        {
            goodsInfo GI = new goodsInfo();
            goodsInfoManager GIManager = new goodsInfoManager();
            goods gs = new goods();
            goodsManager gsManager=new goodsManager();
            string expiresDate = fc["expiresDate"];
            string expiresDate2 = fc["expiresDate2"];
            string startNum = fc["startNum"];
            string endNum = fc["endNum"];
            bool select = Convert.ToBoolean(fc["select"]);
            int pageSize = Convert.ToInt32(fc["pageSize"]);
            StringBuilder sb = new StringBuilder();
            List<goodsInfo> listAll = null;
            if (select)
            {
                listAll = GIManager.GetByTime(expiresDate, expiresDate2, startNum, endNum).ToList();
                if (listAll.Count() > 0)
                {
                    for (int i = 0; i < listAll.Count(); i++)
                    {
                        if (i < pageSize)
                        { 
                            gs = gsManager.getBygoodsId(listAll[i].gid);
                            sb.Append("<tr onmouseover=\"this.style.backgroundColor='#BCD2EE';cursor='default'\" onmouseout=\"this.style.backgroundColor='white'\">");
                            sb.Append("<td>" + listAll[i].gid + "</td>");
                            if (gs != null)
                            {
                                sb.Append("<td>" + gs.goodsName + "</td>");
                            }
                            else
                            {
                                sb.Append("<td></td>");
                            }
                            sb.Append("<td>" + listAll[i].goodscount + "</td>");
                            sb.Append("<td>" + listAll[i].goodsprice + "</td>");
                            sb.Append("<td>" + listAll[i].selltime + "</td>");
                            sb.Append("<td>" + listAll[i].zhekou + "</td>");
                           
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                listAll = GIManager.GetAll().ToList();
                for (int i = 0; i < listAll.Count(); i++)
                {
                    if (i < pageSize)
                    {
                        gs = gsManager.getBygoodsId(listAll[i].gid);
                        sb.Append("<tr onmouseover=\"this.style.backgroundColor='#BCD2EE';cursor='default'\" onmouseout=\"this.style.backgroundColor='white'\">");
                        sb.Append("<td>" + listAll[i].gid + "</td>");
                        if (gs != null)
                        {
                            sb.Append("<td>" + gs.goodsName + "</td>");
                        }
                        else
                        {
                            sb.Append("<td></td>");
                        }
                        sb.Append("<td>" + listAll[i].goodscount + "</td>");
                        sb.Append("<td>" + listAll[i].goodsprice + "</td>");
                        sb.Append("<td>" + listAll[i].selltime + "</td>");
                        sb.Append("<td>" + listAll[i].zhekou + "</td>");
                        sb.Append("</tr>");
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return Content(sb.ToString());
        }
        //实现分页
        public ActionResult newPage(FormCollection fc)
        {
            int pageNum = Convert.ToInt32(fc["pageNum"]);
            int pageSize = Convert.ToInt32(fc["pageSize"]);
            int maxPage = pageNum * pageSize - 1;
            int minPage = maxPage - pageSize + 1;
            string expiresDate = fc["expiresDate"];
            string expiresDate2 = fc["expiresDate2"];
            string startNum = fc["startNum"];
            string endNum = fc["endNum"];
            bool select = Convert.ToBoolean(fc["select"]);
            goodsInfo GI = new goodsInfo();
            goodsInfoManager GIManager = new goodsInfoManager();
            goods gs = new goods();
            goodsManager gsManager = new goodsManager();
            StringBuilder sb = new StringBuilder();
            List<goodsInfo> listAll = null;
            if (select)
            {
                listAll = GIManager.GetByTime(expiresDate, expiresDate2, startNum, endNum).ToList();
                if (listAll.Count() > 0)
                {
                    for (int i = minPage; i <= maxPage; i++)
                    {
                        try
                        {
                            gs = gsManager.getBygoodsId(listAll[i].gid);
                            sb.Append("<tr onmouseover=\"this.style.backgroundColor='#BCD2EE';cursor='default'\" onmouseout=\"this.style.backgroundColor='white'\">");
                            sb.Append("<td>" + listAll[i].gid + "</td>");
                            if (gs != null)
                            {
                                sb.Append("<td>" + gs.goodsName + "</td>");
                            }
                            else
                            {
                                sb.Append("<td></td>");
                            }
                            sb.Append("<td>" + listAll[i].goodscount + "</td>");
                            sb.Append("<td>" + Convert.ToDecimal(listAll[i].goodsprice).ToString("#0.00") + "</td>");
                            sb.Append("<td>" +listAll[i].selltime + "</td>");
                            sb.Append("<td>" + listAll[i].zhekou + "</td>");
                        }

                        catch
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                listAll = GIManager.GetAll().ToList();
                for (int i = minPage; i <= maxPage; i++)
                {
                    try
                    {
                        gs = gsManager.getBygoodsId(listAll[i].gid);
                        sb.Append("<tr onmouseover=\"this.style.backgroundColor='#BCD2EE';cursor='default'\" onmouseout=\"this.style.backgroundColor='white'\">");
                        sb.Append("<td>" + listAll[i].gid + "</td>");
                        if (gs != null)
                        {
                            sb.Append("<td>" + gs.goodsName + "</td>");
                        }
                        else
                        {
                            sb.Append("<td></td>");
                        }
                        sb.Append("<td>" + listAll[i].goodscount + "</td>");
                        sb.Append("<td>" + Convert.ToDecimal(listAll[i].goodsprice).ToString("#0.00") + "</td>");
                        sb.Append("<td>" + listAll[i].selltime + "</td>");
                        sb.Append("<td>" + listAll[i].zhekou + "</td>");
                    }
                    catch
                    {
                        break;
                    }
                }
            }
            return Content(sb.ToString());
        }

        //最大页数
        public ActionResult Data_Load_maxPage(FormCollection fc)
        {
            goodsInfo GI = new goodsInfo();
            goodsInfoManager GIManager = new goodsInfoManager();

            int pageSize = Convert.ToInt32(fc["pageSize"]);
            string expiresDate = fc["expiresDate"];
            string expiresDate2 = fc["expiresDate2"];
            bool select = Convert.ToBoolean(fc["select"]);
            string startNum = fc["startNum"];
            string endNum = fc["endNum"];
            int count = 0;
            List<goodsInfo> listAll = null;
            if (select)
            {
                listAll = GIManager.GetByTime(expiresDate, expiresDate2, startNum, endNum).ToList();
                if (listAll.Count() > 0)
                {
                    count = listAll.Count();
                }
            }
            else
            {
                listAll = GIManager.GetAll().ToList();
                count = listAll.Count();
            }
            string oop = Math.Ceiling((double)count / pageSize).ToString();
            return Content(oop);
        }

    }
}
